﻿using System;
using System.Collections.Generic;
using XCI.Sys.Model;
using XCI.Core;
using XCI.Helper;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统应用服务
    /// </summary>
    public class AppService : BaseService<SysApp>
    {
        public static readonly AppService Instance = new AppService();
        protected override string RootUrl => "/api/sys/app/";

        /// <summary>
        /// 新建应用模型
        /// </summary>
        public SysApp New()
        {
            return new SysApp
            {
                Id = NewId(),
                Secret = BuildSecret(),
                Status = true
            };
        }

        /// <summary>
        /// 复制应用模型
        /// </summary>
        /// <param name="original">原模型</param>
        public SysApp Copy(SysApp original)
        {
            if (original == null) throw new ArgumentNullException(nameof(original));
            var model = original?.Clone();
            model.Id = NewId();
            model.Secret = BuildSecret();
            return model;
        }

        /// <summary>
        /// 生成密钥
        /// </summary>
        public string BuildSecret()
        {
            return EncryptHelper.SymmetricEncrypt(StringHelper.Guid(), EncryptHelper.SecretKey);
        }

        /// <summary>
        /// 检查应用名称是否存在
        /// </summary>
        /// <param name="name">应用名称</param>
        public bool ExistByName(string name)
        {
            return ExistByNameCore(name);
        }

        /// <summary>
        /// 新建应用
        /// </summary>
        /// <param name="model">应用模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Insert(SysApp model)
        {
            return InsertCore(model);
        }

        /// <summary>
        /// 修改应用
        /// </summary>
        /// <param name="model">应用模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Update(SysApp model)
        {
            return UpdateCore(model);
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <param name="status">状态,1启用;0禁用</param>
        /// <returns>成功返回true</returns>
        public bool UpdateStatus(string ids, bool status)
        {
            return UpdateStatusByIdsCore(ids, status);
        }

        /// <summary>
        /// 删除应用
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <returns>成功返回true</returns>
        public bool Delete(string ids)
        {
            return DeleteByIdsCore(ids);
        }

        /// <summary>
        /// 根据主键获取应用对象
        /// </summary>
        /// <param name="id">应用主键</param>
        public SysApp SelectById(string id)
        {
            return SelectByIdCore(id);
        }

        /// <summary>
        /// 查询应用列表
        /// </summary>
        /// <param name="key">关键字</param>
        /// <param name="status">状态(0:禁用;1:启用;null:全部记录)</param>
        public List<SysApp> SelectList(string key, bool? status)
        {
            var map = Map.New.Add("key", key).Add("status", status);
            return SelectListCore(map);
        }

        /// <summary>
        /// 导出应用列表
        /// </summary>
        /// <param name="key">关键字</param>
        /// <param name="status">状态(0:禁用;1:启用;null:全部记录)</param>
        public byte[] Export(string key, bool? status)
        {
            var map = Map.New.Add("key", key).Add("status", status);
            return ExportCore(map);
        }

        /// <summary>
        /// 刷新服务端应用缓存
        /// </summary>
        public bool RefreshCache()
        {
            return RefreshCacheCore();
        }
    }
}
